<div class="content-box"><!-- Start Content Box -->
    <div class="content-box-header">

        <h3>Sửa loại sản phẩm</h3>

        <ul class="content-box-tabs">

            <li><a href="#">Table</a></li> <!-- href must be unique and match the id of target div -->
            <li><a href="#tab2" class="default-tab">Forms</a></li>
        </ul>

        <div class="clear"></div>

    </div> <!-- End .content-box-header -->

    <div class="content-box-content">



        <div class="content-box-tabs" id="tab2">
            <?php
            $loai='';$loai_id='';$anhl='';$chungloai_id='';
            $hide_show=1;

            if(isset($_POST['loai']))					{$loai=$_POST['loai'];$hide_show=0;}
            if(isset($_POST['loai_id']))				$loai_id=$_POST['loai_id'];
            if(isset($_POST['anhl']))		       		$anhl=$_POST['anhl'];
            if(isset($_POST['chungloai_id']))			$chungloai_id=$_POST['chungloai_id'];

            if($hide_show==1)
            {
                $sql1="SELECT * FROM tbl_loai WHERE id = ".$id."";
                $res1=mysql_query($sql1);
                $row1=mysql_fetch_array($res1);
                ?>
                <form action="" method="post" enctype="multipart/form-data">

                    <fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->

                        <p>
                            <label for="chungloai_id">Chủng loại</label>

                            <?php
                            $sql2='SELECT 	tbl_chungloai.chungloai,
                                            tbl_chungloai.chungloai_id
                                            FROM tbl_chungloai WHERE chungloai_id != "'.$row1['chungloai_id'].'"';
                            $result2=mysql_query($sql2);
                            $row2=mysql_fetch_array($result2);
                            ?>
                            <select name="chungloai_id" id="chungloai_id" style="width:405px;">
                                <?php
                                $sql3='SELECT 	tbl_chungloai.chungloai,
                                            tbl_chungloai.chungloai_id
                                            FROM tbl_chungloai WHERE chungloai_id = "'.$row1['chungloai_id'].'"';
                                $result3=mysql_query($sql3);
                                $row3=mysql_fetch_array($result3);
                                ?>
                                <option value="<?php echo $row3['chungloai_id'];?>"><?php echo $row3['chungloai']?></option>
                                <?php
                                while($row2=mysql_fetch_array($result2))
                                {
                                    ?>
                                    <option value="<?php echo $row2['chungloai_id'];?>"><?php echo $row2['chungloai']?></option>
                                <?php
                                }
                                ?>
                            </select>

                        </p>

                        <p>
                            <label for="loai">Tên loại</label>
                            <input type="text" name="loai" id="loai" value="<?php echo $row1['loai']?>" style="width:400px;"/>
                        </p>
                        <p>
                            <label for="anhl">Ảnh loại</label>
                            <input type="file" name="anhl" id="anhl" />
                        </p>
                        <p>
                            <label for="loai_id">Mã loại</label>
                            <input type="text" name="loai_id" id="loai_id" value="<?php echo $row1['loai_id']?>" style="width:400px;"/>
                        </p>


                        <p>
                            <input class="button" type="submit" value="Submit" name="edit"/>
                            <input type="reset" value="Nhập lại" name="btn_reset"/>
                        </p>

                    </fieldset>

                    <div class="clear"></div><!-- End .clear -->

                </form>
            <?php
            }
            else
            {
                //Define some variables
                $dir = "uploads/"; //Bạn nên thay đổi đường dẫn cho phù hợp
                //Kiều file, Gif, jpeg, zip ::bạn có thể sửa đổi nếu thích
                $types = array("image/gif","image/GIF","image/JPG","image/PNG","image/png","image/jpg","image/JPEG","image/jpeg","application/x-zip-compressed","application/msword","application/vnd.ms-excel");
                //Check to determine if the submit button has been pressed
                if(isset($_POST["edit"])){
                    //Shorten Variables
                    $tmp_name = $_FILES["anhl"]["tmp_name"];
                    $new_name = $_FILES["anhl"]["name"];
                    //Check MIME Type
                    if (in_array($_FILES["anhl"]["type"], $types)){
                        //Move file from tmp dir to new location
                        move_uploaded_file($tmp_name,'../'.$dir.$new_name);
                        $anhl=$new_name;
                        echo "<script>alert('File {$_FILES["anhl"]["name"]} Thay thế ảnh thành công!');</script>";
                    }else{
                        //Print Error Message
                        echo "<script>alert('{$_FILES["anhl"]["name"]} Không thay đổi ảnh minh họa!');</script>";
                        //Debug
                        $name =  $_FILES["anhl"]["name"];
                        $type =  $_FILES["anhl"]["type"];
                        $size =  $_FILES["anhl"]["size"];
                        $tmp  =  $_FILES["anhl"]["name"];
                        "Name: $name<br/ >Type: $type<br />Size: $size<br />Tmp: $tmp";
                    }
                }
                else{
                    echo 'Không có file upload';
                }
                //Xu ly them du lieu
                //Tao cau lenh SQL
                if($anhl=='')
                {
                    $sql="UPDATE 	tbl_loai
							SET
									loai='".$loai."',
									loai_id='".$loai_id."',
									chungloai_id='".$chungloai_id."'

							WHERE id='".$id."'";
                }else{
                    $sql="UPDATE 	tbl_chungloai
							SET
									loai='".$loai."',
									anhcl='".$anhl."',
									loai_id='".$loai_id."',
									chungloai_id='".$chungloai_id."'

							WHERE id='".$id."'";
                }

                //Thuc thi cau lenh SQL
                $result=mysql_query($sql);
                echo 'Sửa thành công';
                //Chuyen trang ve danh muc san pham
                header('refresh:1; index.php?page=loai');
            }

            ?>
        </div> <!-- End #tab2 -->


        <div class="tab-content" id="tab1"> <!-- This is the target div. id must match the href of this div's tab -->

        </div> <!-- End #tab1 -->
    </div> <!-- End .content-box-content -->

</div> <!-- End .content-box -->